/*******************************************************************************
 * Copyright (c) 2010 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.ssd.vmp;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

/**
 * @author Sylvain Sicard
 * @goal generate
 * 
 */
public class VelocityMojo extends AbstractMojo {

	/**
	 * The project whose project files to create.
	 * 
	 * @parameter expression="${project}"
	 * @required
	 */
	protected MavenProject project;
	
	/**
     * The directory in which files will be stored prior to processing.
     *
     * @parameter expression="${project.build.directory}/classes"
     * @required
     */
    private File workDirectory;

    /**
     * Resources directory.
     *
     * @parameter expression="${project.basedir}/src/main/resources"
     * @required
     */
    private File resourcesDirectory;
    
    /**
     * The project helper used to attach the artifact produced by this plugin to the project.
     * @parameter expression="${component.org.apache.maven.project.MavenProjectHelper}
     */
    private MavenProjectHelper projectHelper;
    
	/**
	 * Remote repositories which will be searched for blocks.
	 * 
	 * @parameter expression="${project.templates}"
	 * @required
	 * @readonly
	 */
	private List<String> templates;

	public void execute() throws MojoExecutionException {
		getLog().info("[velocity] resourcesDir=" + resourcesDirectory);
		Properties p = new Properties();
		p.setProperty("resource.loader", "file");
		p.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.FileResourceLoader");
		p.setProperty("file.resource.loader.path", resourcesDirectory.getAbsolutePath());

		VelocityEngine ve = new VelocityEngine();
		workDirectory.mkdirs();
		try {
			ve.init(p);
			for (String template : templates) {
				getLog().info("processing template : " + template);
				Template t = ve.getTemplate(template);
				VelocityContext context = new VelocityContext();
				context.put("name", "SSD");
				StringWriter writer = new StringWriter();
				t.merge(context, writer);
				// getLog().info(writer.toString());
				int idx = template.lastIndexOf("/"); 
				if (idx > -1) {
					String dirs = template.substring(0,idx);
					String path = workDirectory + File.separator + dirs;
					getLog().info("Create : " + path);
					new File(path).mkdirs();
				}
				File f = new File(workDirectory + File.separator + template);
				f.createNewFile();
				FileWriter fw = new FileWriter(f);
				try {
					fw.write(writer.toString());
				} finally {
					fw.close();
				}
			}
			//projectHelper.addResource(project, workDirectory.getAbsolutePath(), Collections.singletonList("**"),Collections.EMPTY_LIST);
		} catch (Exception e) {
			throw new MojoExecutionException("Erreur Velocity", e);
		}
	}
}
